﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace FifaGamer
{
    /// <summary>
    /// Summary description for Account
    /// </summary>
    public class Account
    {
        public int ID { get; set; }
        public string Firstname {get;set;}
        public string Lastname {get;set;}
        public string Email { 
            get 
            { 
                return m_Email != null ? m_Email.ToLower() : null; 
            } 
            set 
            { 
                m_Email = (value != null) ? value.ToLower() : null; 
            } 
        }
        public string Username { get; set; }
        public string Password { get; set; }
        public DateTime? DateOfBirth { get; set; }
        public string Console { get; set; }
        public string ConsoleID { get; set; }
        public string Phone { get; set; }
        public bool? WhatsApp { get; set; }
        public string SkillLevel { get; set; }
        public Permission Permission { get; set; }

        public string PasswordHash { get { return Authentication.EncryptPassword(this.Password); } }

        private string m_Email;

        public Account(int id, string firstname, string surname, string username, string email, string password, DateTime? dateofbirth, string console, string consoleid, string cellphone, bool? whatsapp, string skilllevel, Permission permission)
        {
            this.ID = id;
            this.Firstname = firstname;
            this.Lastname = surname;
            this.Username = username;
            this.m_Email = email.ToLower();
            this.Password = password;
            this.DateOfBirth = dateofbirth;
            this.Console = console;
            this.ConsoleID = consoleid;
            this.Phone = cellphone;
            this.WhatsApp = whatsapp;
            this.SkillLevel = skilllevel;
            this.Permission = permission;
        }

        public Account(string firstname, string surname, string username, string email, DateTime? dateofbirth, string console, string consoleid, string cellphone, bool? whatsapp, string skilllevel)
            : this(0, firstname, surname, username, email, null, dateofbirth, console, consoleid, cellphone, whatsapp, skilllevel, Permission.Player)
        {
        }

        public Account(Database.Account account)
            : this(account.ID, account.Firstname, account.Lastname, account.Username, account.Email, account.Password, account.DateOfBirth, account.Console, account.ConsoleUsername, account.Phone, account.WhatsApp, account.SkillLevel, (FifaGamer.Permission) account.Permission)
        { 
        }

        public int Create()
        {
            if (!Account.Verify_Exists(this.Email, this.Username))
            {                
                using (var db = DBManager.GamerDB())
                {
                    var account = new Database.Account();
                    account.Email = this.Email;
                    account.Username = this.Username;
                    account.Password = Authentication.GeneratePassword(8);
                    account.Firstname = this.Firstname;
                    account.Lastname = this.Lastname;
                    account.Phone = this.Phone;
                    account.WhatsApp = this.WhatsApp;
                    account.DateOfBirth = this.DateOfBirth;
                    account.Console = this.Console;
                    account.ConsoleUsername = this.ConsoleID;
                    account.SkillLevel = this.SkillLevel;
                    account.Active = false;
                    account.Permission = (int)Permission.Player;

                    db.Accounts.InsertOnSubmit(account);
                    db.SubmitChanges();

                    return db.Accounts.Where(x => x.Username == this.Username).First().ID;
                }
            }
            return 0;
        }

        #region Public Static Methods
        /// <summary>
        /// Checks that an account exists
        /// </summary>
        /// <param name="email"></param>
        /// <param name="username"></param>
        /// <returns></returns>
        public static bool Verify_Exists(string email, string username)
        {
            //check the DB whether the account exists;
            using (var db = DBManager.GamerDB())
            {
                var accounts = db.Accounts.Where(account => account.Email == email || account.Username.ToLower().Trim() == username.ToLower().Trim());
                return accounts.Any();
            }            
        }

        /// <summary>
        /// Verifies whether the username and password works
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public static bool Verify_Account(string username, string password)
        {
            if (!string.IsNullOrWhiteSpace(username))
            {
                using (var db = DBManager.GamerDB())
                {
                    var accounts = db.Accounts.ToList();
                    var verify = accounts.Where(x => x.Username.ToLower().Trim() == username.ToLower().Trim() && x.Password == password && x.Active == true);

                    return verify.Any();
                }
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// Verify user, but with hashed password
        /// </summary>
        /// <param name="username"></param>
        /// <param name="hash"></param>
        /// <returns></returns>
        public static bool Verify_AccountHash(string username, string hash)
        {
            var password = Authentication.DecryptPassword(hash);
            return Verify_Account(username, password);
        }

        /// <summary>
        /// Gets an account
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public static Account Get_Account(string username)
        {
            Account account = null;
            
            if (!string.IsNullOrWhiteSpace(username))
            {
                using (var db = DBManager.GamerDB())
                {
                    var dbaccounts = db.Accounts.Where(accounts => accounts.Username.ToLower().Trim() == username.ToLower().Trim()).ToList();
                    if (dbaccounts.Any())
                    {
                        account = new Account(dbaccounts.First());
                    }
                }
            }

            return account;
        }

        #endregion
    }
}